function showDiv(div,id) {
	var d = document.getElementById(id);
	if(d.getElementsByTagName('tbody').length!=0 & d.style.display == "none"){
		clearAllDiv();
		d.style.display = "";
		div.getElementsByTagName('span')[0].className="tab_selected";
		var pos=getElementPos(div);
		d.style.left=(pos.x-7-d.clientWidth+div.clientWidth)+'px';
		d.style.top=(pos.y-d.scrollHeight-2)+'px';
	}else{
		clearAllDiv();
		div.getElementsByTagName('span')[0].className="tab_unselected";
		d.style.display = "none";
	}
}

function clearAllDiv() {
	if(document.getElementById("statusFooterDiv").style.display ==''){
		document.getElementById("footer_issues").getElementsByTagName('span')[0].className="tab_unselected";
		document.getElementById("statusFooterDiv").style.display = 'none';
	}
	if(document.getElementById("notificationFooterDiv").style.display ==''){
		document.getElementById("footer_notifications").getElementsByTagName('span')[0].className="tab_unselected";
		document.getElementById("notificationFooterDiv").style.display = 'none';
	}
	if(document.getElementById("requestFooterDiv").style.display ==''){
		document.getElementById("footer_requests").getElementsByTagName('span')[0].className="tab_unselected";
		document.getElementById("requestFooterDiv").style.display = 'none';
	}
}

function clearRequest(url,requestId,prefix){
	coolio.xhrGet({
		url: url,
   		handleAs: "json",
   		content: {
   			requestId: requestId
	    },
		handle:function(data){
			var requestTbody=document.getElementById(prefix+requestId);
			requestTbody.parentNode.removeChild(requestTbody);   
			var count=document.getElementById('request_count').innerHTML;
			count-=1;
			document.getElementById('request_count').innerHTML=count;
			document.getElementById("requestFooterDiv").style.display ='none';
			showDiv(document.getElementById('footer_requests'),'requestFooterDiv');
		},
		error: function(response, ioArgs){
	    	alert("xhr get failed:");
		    return response;
		}
	});
}

function new_request(tableId,prefix,basePath,requestId,requesterName,requesterId,requestType,updateTime){
	var table=document.getElementById(tableId);
	var tbody=document.createElement('tbody');
	tbody.id=prefix+requestId;
	var tr=document.createElement('tr');
	var nameTd=document.createElement('td');
	var nameA=document.createElement('a');
	nameA.href=basePath+"user/get_user?userId="+requesterId;
	nameA.innerHTML=requesterName;
	nameTd.appendChild(nameA);
	var typeTd=document.createElement('td');
	typeTd.innerHTML="requests to";
	var contentTd=document.createElement('td');
	var contentA=document.createElement('a');
	contentA.href=basePath+"request/get_request?requestId="+requestId;
	contentA.innerHTML=requestType;
	contentTd.appendChild(contentA);
	var timeTd=document.createElement('td');
	timeTd.innerHTML=updateTime;
	
	tr.appendChild(nameTd);
	tr.appendChild(typeTd);
	tr.appendChild(contentTd);
	tr.appendChild(timeTd);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	document.getElementById("requestFooterDiv").style.display = 'none';
}

function new_notification(tableId,prefix,basePath,notificationIds,modifiers,modificationType,notificationType,objectName,objectId,requestTime){
	var table=document.getElementById(tableId);
	var tbody=document.createElement('tbody');
	tbody.id=prefix+notificationIds;
	var tr=document.createElement('tr');
	var nameTd=document.createElement('td');
	for(var i=0;i<modifiers.length;i++){
		var nameA=document.createElement('a');
		nameA.href=basePath+"user/get_user?userId="+modifiers[i].id;
		nameA.innerHTML=modifiers[i].name;
		nameTd.appendChild(nameA);
	}
	var typeTd=document.createElement('td');
	if(modificationType=='REPLY'){
		typeTd.innerHTML='reply you on';
	}
	var contentTd=document.createElement('td');
	var contentA=document.createElement('a');
	if(notificationType=='STATUS'){
		contentA.href=basePath+"status/get_status?statusId="+objectId+"&notificationIds="+notificationIds;
	}else if(notificationType=='SESSION'){
		contentA.href=basePath+"question/"+modificationType+"?sessionId="+objectId+"&notificationIds="+notificationIds;
	}
	contentA.innerHTML=objectName;
	contentTd.appendChild(contentA);
	var timeTd=document.createElement('td');
	timeTd.innerHTML=requestTime;
	var clearTd=document.createElement('td');
	var clearA=document.createElement('a');
	clearA.href="#";
	clearA.onclick=function(){
		coolio.xhrGet({
			url: basePath+"notification/clear_notification",
	   		handleAs: "json",
	   		content: {
	   			notificationIds: notificationIds
		    },
			handle:function(data){
				var notificationTbody=document.getElementById(prefix+notificationIds);
				notificationTbody.parentNode.removeChild(notificationTbody);   
				var count=document.getElementById('notification_count').innerHTML;
				count-=1;
				document.getElementById('notification_count').innerHTML=count;
				document.getElementById("notificationFooterDiv").style.display ='none';
				showDiv(document.getElementById('footer_notifications'),'notificationFooterDiv');
			},
			error: function(response, ioArgs){
		    	alert("xhr get failed:");
			    return response;
			}
		});
	};
	clearA.innerHTML="clear";
	clearTd.appendChild(clearA);
	tr.appendChild(nameTd);
	tr.appendChild(typeTd);
	tr.appendChild(contentTd);
	tr.appendChild(timeTd);
	tr.appendChild(clearTd);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	document.getElementById("notificationFooterDiv").style.display = 'none';
}

function get_update(basePath){
coolio.xhrGet({
	url: basePath+"get_updates",
	handleAs: "json",
	handle:function(data){
		document.getElementById('request_count').innerHTML=data.requests.length;
		if(data.requests.length!=request_count){
			if(request_count!=-1 & data.requests.length>request_count){
				document.getElementById('request_count').parentNode.className="tab_alert tab_unselected";
			}
			for(var i=0;i<data.requests.length;i++){
				if(document.getElementById('footerRequest'+data.requests[i].id)==null){
					new_request('requestTable','footerRequest',basePath,data.requests[i].id,data.requests[i].requesterName,data.requests[i].requesterId,data.requests[i].requestType,data.requests[i].requestTime);
				}
			}
			var requestTBodys=document.getElementById('requestTable').getElementsByTagName('tbody');
			for(var j=0;j<requestTBodys.length;j++){
				var exist=false;
				var id=requestTBodys[j].getAttribute('id').substring(13);
				for(var i=0;i<data.requests.length;i++){
					if(data.requests[i].id==id){
						exist=true;
						break;
					}
				}
				if(!exist){
					var request=requestTBodys[j];
					request.parentNode.removeChild(request);
				}
			}
		}
		request_count=data.requests.length;
		document.getElementById('notification_count').innerHTML=data.notifications.length;
		if(data.notifications.length!=notification_count){
			if(notification_count!=-1 & data.notifications.length>notification_count){
				document.getElementById('notification_count').parentNode.className="tab_alert tab_unselected";
			}
			for(var i=0;i<data.notifications.length;i++){
				if(document.getElementById('footerNotification'+data.notifications[i].ids)==null){
					new_notification('notificationTable','footerNotification',basePath,data.notifications[i].ids,data.notifications[i].modifiers,data.notifications[i].modificationType,data.notifications[i].notificationType,data.notifications[i].objectName,data.notifications[i].objectId,data.notifications[i].updateTime);
				}
			}
			var notificationTBodys=document.getElementById('notificationTable').getElementsByTagName('tbody');
			for(var j=0;j<notificationTBodys.length;j++){
				var exist=false;
				var id=notificationTBodys[j].getAttribute('id').substring(18);
				for(var i=0;i<data.notifications.length;i++){
					if(data.notifications[i].ids==id){
						exist=true;
						break;
					}
				}
				if(!exist){
					var notification=notificationTBodys[j];
					notification.parentNode.removeChild(notification);
				}
			}
		}
		notification_count=data.notifications.length;
	},
	error: function(response, ioArgs){
    	alert("xhr get failed:");
	    return response;
	}
});
}

function getElementPos(el) {
	var ua = navigator.userAgent.toLowerCase();
	var isOpera = (ua.indexOf('opera') != -1);
	var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
	if(el.parentNode === null || el.style.display == 'none') {
	  return false;
	}     
	var parent = null;
	var pos = [];    
	var box;    
	if(el.getBoundingClientRect)    //IE
	{         
	   box = el.getBoundingClientRect();
	   var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	   var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
	  return {x:box.left + scrollLeft, y:box.top + scrollTop};
	}else if(document.getBoxObjectFor)    // gecko   
	{
	   box = document.getBoxObjectFor(el);
	   var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
	   var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
	   pos = [box.x - borderLeft, box.y - borderTop];
	} else    // safari & opera   
	{
	   pos = [el.offsetLeft, el.offsetTop];
	   parent = el.offsetParent;    
	   if (parent != el) {
	    while (parent) {
	     pos[0] += parent.offsetLeft;
	     pos[1] += parent.offsetTop;
	     parent = parent.offsetParent;
	    }
	   }  
	   if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) {
	    pos[0] -= document.body.offsetLeft;
	    pos[1] -= document.body.offsetTop;        
	   }   
	}             
	if (el.parentNode) {
	   parent = el.parentNode;
	} else {
	   parent = null;
	}
	while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML') { // account for any scrolled ancestors
	   pos[0] -= parent.scrollLeft;
	   pos[1] -= parent.scrollTop;
	   if (parent.parentNode) {
	    parent = parent.parentNode;
	   } else {
	    parent = null;
	   }
	}
	return {x:pos[0], y:pos[1]};
}